import {
    Scene,
    PerspectiveCamera,
    WebGLRenderer,
    GridHelper,
    AxesHelper,
    SpotLight,
    Mesh,
    SpotLightHelper,
    AmbientLight,
    SphereGeometry,
    BoxHelper,
    MeshBasicMaterial,
    CameraHelper,
    PolarGridHelper,
} from 'three';

export default class LChart {
    private dom: string;
    private scene: Scene;
    private camera: PerspectiveCamera;
    private renderer: WebGLRenderer;
    private lightOpt: LightOpt = { x: 10, y: 30, z: 20 };
    private cameraOpt: LightOpt = { x: 1, y: 2, z: 4 };

    constructor(dom: string, lightOpt: LightOpt, cameraOpt: CameraOpt) {
        this.lightOpt = Object.assign(this.lightOpt, lightOpt);
        this.cameraOpt = Object.assign(this.cameraOpt, cameraOpt?.position);
        const { fov = 30, width = 500, height = 500, near = 1, far = 1000, position } = cameraOpt;
        this.dom = dom;
        // 添加场景
        this.scene = new Scene();

        // 添加相机
        this.camera = new PerspectiveCamera(fov, width / height, near, far);
        // 设置相机位置
        this.setCamera();
        // 开灯
        this.setLight()
        // 渲染器
        this.renderer = new WebGLRenderer({
            antialias: true,
            alpha: true,
        });
        // 设置背景色  透明
        this.renderer.setClearColor(0xffffff, 0);
        // 设置可视区域
        this.renderer.setSize(width, height);

        // 获取dom节点，并将渲染结果插入dom
        document.getElementsByClassName(this.dom)[0].appendChild(this.renderer.domElement);
        // 渲染
        this.renderer.render(this.scene, this.camera);

        // 辅助措施
        // 添加坐标系帮助
        // this.buildAuxSystem();
        // 相机帮助
        // this.buildCamera();
        // 添加可视区域盒子辅助
        // this.buildBox();
        // 灯光帮助
        // this.buildLight();
        // this.buildGrid()
    }
    /**
     * @description 画饼图
     * @param data
     * @param ladder
     * @param separation
     */
    public setChartData(mesh: Mesh) {
        this.scene.add(mesh);
        this.renderer.render(this.scene, this.camera);
    }
    /**
     * @description 开灯
     */
    private setLight() {
        const ambientLight = new AmbientLight(0x555555);
        this.scene.add(ambientLight);
        const { x, y, z } = this.lightOpt;
        const spotLight = new SpotLight(0xffffff);
        spotLight.position.set(x, y, z);
        this.scene.add(spotLight);
    }

    /**
     * @description 设置相机位置
     */
    private setCamera() {
        const { x, y, z } = this.cameraOpt;
        this.camera.position.set(x, y, z)
        this.camera.lookAt(this.scene.position);
    }
    /**
     * @description 相机帮助
     */
    private buildCamera() {
        const camera = new PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        const helper = new CameraHelper(camera);
        this.scene.add(helper);
    }
    /**
     * @description 灯光帮助
     */
    private buildLight() {
        const { x, y, z } = this.lightOpt;
        const spotLight = new SpotLight(0xffffff);
        spotLight.position.set(x, y, z);
        this.scene.add(spotLight);
        const spotLightHelper = new SpotLightHelper(spotLight);
        this.scene.add(spotLightHelper);

        // const pointLight = new PointLight( 0xff0000, 1, 100 );
        // pointLight.position.set( x, y, z );
        // this.scene.add( pointLight );
        // const sphereSize = 1;
        // const pointLightHelper = new PointLightHelper( pointLight, sphereSize );
        // this.scene.add( pointLightHelper );
    }
    /**
     * @description 设置坐标系辅助线
     */
    private buildAuxSystem() {
        const gridHelper = new GridHelper(320, 32);
        const axesHelper = new AxesHelper(320);
        this.scene.add(gridHelper);
        this.scene.add(axesHelper);
    }

    /**
     * @description 构建可视区域盒子
     */
    private buildBox() {
        const sphere = new SphereGeometry();
        const object = new Mesh(sphere, new MeshBasicMaterial());
        const box = new BoxHelper(object, 0xffff00);
        this.scene.add(box);

        // const box = new Box3();
        // box.setFromCenterAndSize( new Vector3( 1, 1, 1 ), new Vector3( 2, 1, 3 ) );

        // const helper = new Box3Helper( box );
        // this.scene.add( helper );
    }
    /**
     * @description 网格线帮助
     */
    private buildGrid() {
        // const size = 10;
        // const divisions = 10;

        // const gridHelper = new GridHelper( size, divisions );
        // this.scene.add( gridHelper );

        const radius = 10;
        const radials = 16;
        const circles = 8;
        const divisions = 64;

        const helper = new PolarGridHelper( radius, radials, circles, divisions );
        this.scene.add( helper );
    }

}
